home *** CD-ROM | disk | FTP | other *** search
- /******************************* <+>***************************
- ** DTA
- *************************************************************
- **
- ** $Id: Collection.h,v 1.6 1994/03/01 23:01:20 dta Exp $
- **
- ** $Source: /cvs/lib/odmg/Collection.h,v $
- **
- ** What @(#):
- **
- ** Author: Dale T. Anderson
- **
- ******************************* <+>***************************/
-
- #ifndef Collection_h
- #define Collection_h
-
- #include <Odmg/Ref.h>
- #include <Odmg/Iterator.h>
-
- template <class T> class Collection;
-
- template <class T>
- class Collection
- {
- protected:
-
- Position m_count;
- boolean m_is_ordered;
- boolean m_allows_duplicates;
-
- public:
-
- Position cardinality (void) const;
- boolean empty (void) const;
-
- boolean ordered (void) const;
- boolean allows_duplicates (void) const;
-
- protected:
-
- Collection (void);
-
- public:
-
- virtual ~Collection (void);
-
- boolean operator == (Ref <Collection <T> >) const;
- boolean operator != (Ref <Collection <T> >) const;
-
- Collection <T> &operator = (Ref <Collection <T> >);
-
- Ref <Collection <T> > copy (void) const;
-
- Ref <Iterator <T> > create_iterator (boolean stable = FALSE) const;
-
- virtual void insert_element (Ref <T>);
- void operator += (Ref <T>);
-
- virtual void remove_element (Ref <T>);
- void operator -= (Ref <T>);
-
- virtual void remove_element_at (Iterator <T> &);
- virtual void replace_element_at (Ref <T>, const Iterator <T> &);
- virtual Ref <T> retrieve_element_at (const Iterator <T> &);
-
- virtual Ref <T> select_element (const char *);
- virtual Iterator <T> select (Iterator <T> , const char *);
- virtual int query (Ref <Collection <T> > &, const char *);
-
- virtual boolean exists_element (const char *) const;
- virtual boolean contains_element (Ref <T>) const;
-
- // Additions
-
- Ref <Collection <T> > _union (const Ref <Collection <T> >) const;
- Ref <Collection <T> > intersection (const Ref <Collection <T> >) const;
- Ref <Collection <T> > difference (const Ref <Collection <T> >) const;
-
- Ref <Collection <T> > operator + (const Ref <Collection <T> >) const;
- Ref <Collection <T> > operator * (const Ref <Collection <T> >) const;
- Ref <Collection <T> > operator - (const Ref <Collection <T> >) const;
-
- Ref <Collection <T> > operator += (const Ref <Collection <T> >);
- Ref <Collection <T> > operator *= (const Ref <Collection <T> >);
- Ref <Collection <T> > operator -= (const Ref <Collection <T> >);
-
- boolean is_subset_of (const Ref <Collection <T> >) const;
- boolean is_proper_subset_of (const Ref <Collection <T> >) const;
- boolean is_superset_of (const Ref <Collection <T> >) const;
- boolean is_proper_superset_of (const Ref <Collection <T> >) const;
-
- virtual Ref <T> retrieve_element_at (const Position);
-
- void remove_all (void);
- void delete_all (void);
-
- virtual Ref <Collection <T> > create (void) const;
- };
-
-
- //
- // Methods
- //
-
- template <class T>
- Position Collection <T>::cardinality (void) const
- {
- return m_count;
- }
-
- template <class T>
- boolean Collection <T>::empty (void) const
- {
- return m_count == 0;
- }
-
- template <class T>
- boolean Collection <T>::ordered (void) const
- {
- return m_is_ordered;
- }
-
- template <class T>
- boolean Collection <T>::allows_duplicates (void) const
- {
- return m_allows_duplicates;
- }
-
- template <class T>
- Collection <T>::Collection (void) :
- m_is_ordered (TRUE),
- m_allows_duplicates (TRUE),
- m_count (0)
- {
- }
-
- template <class T>
- Collection <T>::~Collection (void)
- {
- }
-
- template <class T>
- boolean Collection <T>::operator == (Ref <Collection <T> > c) const
- {
- if (cardinality () != c->cardinality ())
- return FALSE;
-
- Iterator <T> iter1 (c);
- Iterator <T> iter2 (this);
-
- Ref <T> ref1;
- Ref <T> ref2;
-
- boolean isequal = TRUE;
-
- while (isequal && iter1.next (ref1) && iter2.next (ref2))
- if (ref1 != ref2)
- isequal = FALSE;
-
- return isequal;
- }
-
- template <class T>
- boolean Collection <T>::operator != (Ref <Collection <T> > c) const
- {
- return !(c == (Ref <Collection <T> > &) *this);
- }
-
- template <class T>
- Collection <T> &Collection <T>::operator = (Ref <Collection <T> > c)
- {
- remove_all ();
-
- Iterator <T> iter (c);
- Ref <T> ref;
-
- while (iter.next (ref))
- insert_element (ref);
-
- return *this;
- }
-
- template <class T>
- Ref <Collection <T> > Collection <T>::copy (void) const
- {
- Ref <Collection <T> > c (create ());
-
- Iterator <T> iter ((Collection <T> *) this);
- Ref <T> ref;
-
- while (iter.next (ref))
- c->insert_element (ref);
-
- return c;
- }
-
- template <class T>
- Ref <Iterator <T> > Collection <T>::create_iterator (boolean) const
- {
- return Ref <Iterator <T> > (new Iterator <T> ((Collection <T> *) this));
- }
-
- template <class T>
- void Collection <T>::insert_element (Ref <T>)
- {
- cerr << "insert_element - not implemented." << endl;
- }
-
- template <class T>
- void Collection <T>::operator += (Ref <T> ref)
- {
- insert_element (ref);
- }
-
- template <class T>
- void Collection <T>::remove_element (Ref <T>)
- {
- cerr << "remove_element - not implemented." << endl;
- }
-
- template <class T>
- void Collection <T>::operator -= (Ref <T> ref)
- {
- remove_element (ref);
- }
-
- template <class T>
- void Collection <T>::remove_element_at (Iterator <T> &iter)
- {
- Position position = iter.get_position ();
- if (position >= 0)
- iter.set_position (position - 1);
- remove_element (retrieve_element_at (position));
- }
-
- template <class T>
- void Collection<T>::replace_element_at (Ref<T>, const Iterator <T> &)
- {
- cerr << "replace_element_at - not implemented" << endl;
- }
-
- template <class T>
- Ref <T> Collection <T>::retrieve_element_at (const Iterator <T> &iter)
- {
- return retrieve_element_at (iter.get_position ());
- }
-
- template <class T>
- Ref <T> Collection <T>::select_element (const char *)
- {
- cerr << "select_element_at - not implemented." << endl;
- return Ref <T> (NULL);
- }
-
- template <class T>
- Iterator <T> Collection <T>::select (Iterator <T> iter, const char *)
- {
- cerr << "select - not implemented." << endl;
- return iter;
- }
-
- template <class T>
- int Collection <T>::query (Ref <Collection <T> >&, const char *)
- {
- cerr << "query - not implemented." << endl;
- return -1;
- }
-
- template <class T>
- boolean Collection <T>::exists_element (const char *) const
- {
- cerr << "exists_element - not implemented." << endl;
- return FALSE;
- }
-
- template <class T>
- boolean Collection <T>::contains_element (Ref <T> target) const
- {
- Iterator <T> iter (this);
- Ref <T> ref;
-
- while (iter.next (ref))
- if (ref == target)
- return TRUE;
-
- return FALSE;
- }
-
- //
- // Additions
- //
-
- template <class T>
- Ref <Collection <T> > Collection<T>::_union (const Ref <Collection <T> > c1) const
- {
- Ref <Collection <T> > c2 (copy ());
- Iterator <T> iter (c1);
- Ref <T> ref;
-
- while (iter.next (ref))
- if (c2->allows_duplicates () || !c2->contains_element (ref))
- c2->insert_element (ref);
-
- return c2;
- }
-
- template <class T>
- Ref <Collection <T> > Collection<T>::intersection (const Ref <Collection <T> > c1) const
- {
- Ref <Collection <T> > c2 (create ());
- Iterator <T> iter (this);
- Ref <T> ref;
-
- while (iter.next (ref))
- if (c1->contains_element (ref))
- c2->insert_element (ref);
-
- return c2;
- }
-
- template <class T>
- Ref <Collection <T> > Collection<T>::difference (const Ref <Collection <T> > c1) const
- {
- Ref <Collection <T> > c2 (create ());
- Iterator <T> iter (this);
- Ref <T> ref;
-
- while (iter.next (ref))
- if (!c1->contains_element (ref))
- c2->insert_element (ref);
-
- return c2;
- }
-
- template <class T>
- Ref <Collection <T> > Collection<T>::operator += (const Ref <Collection <T> > c1)
- {
- Iterator <T> iter (c1);
- Ref <T> ref;
-
- while (iter.next (ref))
- if (allows_duplicates () || !contains_element (ref))
- insert_element (ref);
-
- return Ref <Collection <T> > (this);
- }
-
- template <class T>
- Ref <Collection <T> > Collection<T>::operator *= (const Ref <Collection <T> > c1)
- {
- Iterator <T> iter (this);
- Ref <T> ref;
-
- while (iter.next (ref))
- if (!c1->contains_element (ref))
- remove_element_at (iter);
-
- return Ref <Collection <T> > (this);
- }
-
- template <class T>
- Ref <Collection <T> > Collection<T>::operator -= (const Ref <Collection <T> > c1)
- {
- Iterator <T> iter (c1);
- Ref <T> ref;
-
- while (iter.next (ref))
- while (contains_element (ref))
- remove_element (ref);
-
- return Ref <Collection <T> > (this);
- }
-
- template <class T>
- Ref <Collection <T> > Collection<T>::operator + (const Ref <Collection <T> > c) const
- {
- return _union (c);
- }
-
- template <class T>
- Ref <Collection <T> > Collection<T>::operator * (const Ref <Collection <T> > c) const
- {
- return intersection (c);
- }
-
- template <class T>
- Ref <Collection <T> > Collection<T>::operator - (const Ref <Collection <T> > c) const
- {
- return difference (c);
- }
-
- template <class T>
- boolean Collection <T>::is_subset_of (const Ref <Collection <T> > c) const
- {
- Iterator <T> iter (this);
- Ref <T> ref;
-
- while (iter.next (ref))
- if (!c->contains_element (ref))
- return FALSE;
-
- return TRUE;
- }
-
- template <class T>
- boolean Collection <T>::is_proper_subset_of (const Ref <Collection <T> > c) const
- {
- Iterator <T> iter (this);
- Ref <T> ref;
-
- if (cardinality () != c->cardinality ())
- return FALSE;
-
- while (iter.next (ref))
- if (!c->contains_element (ref))
- return FALSE;
-
- return TRUE;
- }
-
- template <class T>
- boolean Collection <T>::is_superset_of (const Ref <Collection <T> > c) const
- {
- Iterator <T> iter (c);
- Ref <T> ref;
-
- while (iter.next (ref))
- if (!contains_element (ref))
- return FALSE;
-
- return TRUE;
- }
-
- template <class T>
- boolean Collection <T>::is_proper_superset_of (const Ref <Collection <T> > c) const
- {
- Iterator <T> iter (c);
- Ref <T> ref;
-
- if (cardinality () != c->cardinality ())
- return FALSE;
-
- while (iter.next (ref))
- if (!contains_element (ref))
- return FALSE;
-
- return TRUE;
- }
-
- template <class T>
- Ref <T> Collection <T>::retrieve_element_at (const Position)
- {
- cerr << "retrieve_element_at - not implemented" << endl;
- return Ref <T> (NULL);
- }
-
- template <class T>
- void Collection<T>::remove_all (void)
- {
- Iterator <T> iter (this);
- Ref <T> ref;
-
- while (iter.next (ref))
- remove_element_at (iter);
- }
-
- template <class T>
- void Collection<T>::delete_all (void)
- {
- Iterator <T> iter (this);
- Ref <T> ref;
-
- while (iter.next (ref))
- delete (T *) ref;
-
- remove_all ();
- }
-
- template <class T>
- Ref <Collection <T> > Collection <T>::create (void) const
- {
- cerr << "create - not implemented" << endl;
- return Ref <Collection <T> > (NULL);
- }
-
- #endif
-